package q2024_maxConsecutiveAnswers;

public class Solution {
    /*
    考试的最大困扰度
    滑动窗口的典型题
    直接通过1004题的方式即可解决 只不过要遍历两次
     */
    public int maxConsecutiveAnswers(String answerKey, int k) {
        int left = 0, ans = 0, leftCount = 0, rightCount = 0;
        for (int right = 0; right < answerKey.length(); right++) {
            rightCount += answerKey.charAt(right) == 'T' ? 1 : 0;
            while (leftCount < rightCount - k) {
                leftCount += answerKey.charAt(left) == 'T' ? 1 : 0;
                ++left;
            }
            ans = Math.max(ans, right - left + 1);
        }
        leftCount = 0;
        rightCount = 0;
        for (int right = 0; right < answerKey.length(); right++) {
            rightCount += answerKey.charAt(right) == 'F' ? 1 : 0;
            while (leftCount < rightCount - k) {
                leftCount += answerKey.charAt(left) == 'F' ? 1 : 0;
                ++left;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
